package com.syn.synold4s.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.gaoyi.basemodule.baseAdapter.BaseAdapter;
import com.gaoyi.basemodule.baseAdapter.BaseViewHolder;
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.syn.synold4s.R;
import com.syn.synold4s.bean.dto.CctvDto;
import com.syn.synold4s.databinding.CctvRvItemBinding;

public class CctvRvAdapter extends BaseAdapter<CctvRvItemBinding, CctvDto.CCTVModel> {
    private String fullKey = "null";
    public static final String TAG = "CctvRvAdapter";
    private int size;

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public CctvRvAdapter(Context context) {
        super(context);
    }

    @Override
    protected void setBindingVariable(CctvRvItemBinding binding, int position) {

    }

    @Override
    protected void onBindItem(CctvRvItemBinding binding, CctvDto.CCTVModel item, int position) {
        binding.tvCctvTime.setText(item.getCreatedAt() != null ? item.getCreatedAt() : "");
        binding.tvCctvName.setText(item.getName() != null ? item.getName() : "");
        // 多个播放时必须在setUpLazy、setUp和getGSYVideoManager()等前面设置
        binding.videoItemPlayer.setPlayTag(TAG);
        binding.videoItemPlayer.setPlayPosition(position);
        binding.videoItemPlayer.setEnlargeImageRes(R.drawable.icon_enlarge);
        binding.videoItemPlayer.setUpLazy(item.getFlvUrl(), true, null, null, item.getName() != null ? item.getName() : "");
//        }
        // 增加title
        binding.videoItemPlayer.getTitleTextView().setVisibility(View.GONE);
        // 设置返回键
        binding.videoItemPlayer.getBackButton().setVisibility(View.GONE);
        // 设置全屏按键功能
        binding.videoItemPlayer.getFullscreenButton().setOnClickListener(view -> resolveFullBtn(binding.videoItemPlayer));
        binding.videoItemPlayer.setRotateViewAuto(true);
        binding.videoItemPlayer.setLockLand(true);
        binding.videoItemPlayer.setReleaseWhenLossAudio(false);
        binding.videoItemPlayer.setShowFullAnimation(true);
        binding.videoItemPlayer.setIsTouchWiget(false);
        binding.videoItemPlayer.setNeedLockFull(true);
        binding.videoItemPlayer.setStartAfterPrepared(true);
        binding.videoItemPlayer.setNeedShowWifiTip(true);
        //设置加载封面
//        binding.videoItemPlayer.loadCoverImage("", R.drawable.error_img);
        //播放回调
        binding.videoItemPlayer.setVideoAllCallBack(new GSYSampleCallBack() {
            @Override
            public void onQuitFullscreen(String url, Object... objects) {
                super.onQuitFullscreen(url, objects);
                fullKey = "null";
            }

            @Override
            public void onEnterFullscreen(String url, Object... objects) {
                super.onEnterFullscreen(url, objects);
                binding.videoItemPlayer.getCurrentPlayer().getTitleTextView().setText((String) objects[0]);
                fullKey = binding.videoItemPlayer.getKey();
            }

            @Override
            public void onAutoComplete(String url, Object... objects) {
                super.onAutoComplete(url, objects);
                binding.ivCctvState.setBackground(getDrawable(false));
            }

            @Override
            public void onPrepared(String url, Object... objects) {
                super.onPrepared(url, objects);
                Log.e(TAG, "onPrepared: ");
                binding.ivCctvState.setBackground(getDrawable(true));
            }

            @Override
            public void onClickStop(String url, Object... objects) {
                super.onClickStop(url, objects);
                Log.e(TAG, "播放暂停: ");
                binding.ivCctvState.setBackground(getDrawable(false));
            }

            @Override
            public void onClickStartError(String url, Object... objects) {
                super.onClickStartError(url, objects);
                Log.e(TAG, "点击播放错误: ");
                binding.ivCctvState.setBackground(getDrawable(false));
            }

            @Override
            public void onClickResume(String url, Object... objects) {
                super.onClickResume(url, objects);
                Object object = objects[1];
                binding.ivCctvState.setBackground(getDrawable(true));
            }

            @Override
            public void onComplete(String url, Object... objects) {
                super.onComplete(url, objects);
                binding.ivCctvState.setBackground(getDrawable(false));
            }
        });
        //开始播放
        binding.videoItemPlayer.startPlayLogic();
        if (size == 1) {//列表只有一条视频时
            size = 0;
            resolveFullBtn(binding.videoItemPlayer);//自动播放
        }
    }

    private Drawable getDrawable(boolean isPlaying) {
        Drawable mDrawable = ContextCompat.getDrawable(mContext, isPlaying ? R.drawable.oval_green_bg : R.drawable.oval_red_bg);
        if (mDrawable != null) {
            mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
        }
        return mDrawable;
    }

    /**
     * 全屏幕按键处理
     */
    private void resolveFullBtn(final StandardGSYVideoPlayer standardGSYVideoPlayer) {
        standardGSYVideoPlayer.startWindowFullscreen(mContext, false, true);
    }

    public String getFullKey() {
        return fullKey;
    }

    @Override
    protected void onCreateView(CctvRvItemBinding binding, BaseViewHolder<CctvRvItemBinding> holder) {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.cctv_rv_item;
    }
}
